/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.store;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 */
public class GetMessageResult {

    /**
     * 消息列表
     */
    private final List<SelectMappedBufferResult> messageMapedList;

    /**
     * 消息缓冲区列表
     */
    private final List<ByteBuffer> messageBufferList;

    /**
     * 消息的consumequeue的列表
     */
    private final List<Long> messageQueueOffset;

    /**
     * 查询消息的状态，如找到，没找到匹配的等，使用枚举
     */
    private GetMessageStatus status;

    /**
     * 下次拉取消息的起始索引偏移量
     */
    private long nextBeginOffset;

    /**
     * 当前查询消息结果中的消息最小索引偏移量
     */
    private long minOffset;

    /**
     * 当前查询消息结果中的最大索引偏移量
     */
    private long maxOffset;

    /**
     * 缓存总字节数
     */
    private int bufferTotalSize = 0;

    /**
     * 当前结果中包含的消息总数
     */
    private int messageCount = 0;

    /**
     * 是否从slave拉取消息，默认false
     */
    private boolean suggestPullingFromSlave = false;

    private int msgCount4Commercial = 0;

    /**
     * 每条消息的字节数，4k
     */
    private int commercialSizePerMsg = 4 * 1024;

    /**
     * 冷数据大小
     */
    private long coldDataSum = 0L;

    /**
     * 没找到匹配的逻辑队列
     */
    public static final GetMessageResult NO_MATCH_LOGIC_QUEUE =
        new GetMessageResult(
                GetMessageStatus.NO_MATCHED_LOGIC_QUEUE,  // 查询消息结果的状态：没有找到匹配的消息队列
                0,                                        // 下次查询起始偏移量
                0,                                        // 当前结果中消息的最小偏移量
                0,                                        // 当前结果中消息的最大偏移量
                Collections.emptyList(),                  // 消息List集合
                Collections.emptyList(),                  // 消息ByteBuffer的List集合
                Collections.emptyList()                   // 消息队列的偏移量List集合
        );

    /**
     * 构造器：
     * <ol>
     * <li>初始化messageMapedList为100个元素的List集合；</li>
     * <li>初始化messageBufferList为100个元素的List集合；</li>
     * <li>初始化messageQueueOffset为100个元素的List集合。</li>
     * </ol>
     */
    public GetMessageResult() {
        messageMapedList = new ArrayList<>(100);
        messageBufferList = new ArrayList<>(100);
        messageQueueOffset = new ArrayList<>(100);
    }

    /**
     * 构造器：
     * <ol>
     *     <li>将messageMapedList初始化为指定大小的List集合；</li>
     *     <li>将messageBufferList初始化为指定大小的List集合；</li>
     *     <li>将messageQueueOffset初始化为指定大小的List集合。</li>
     * </ol>
     * @param resultSize
     */
    public GetMessageResult(int resultSize) {
        messageMapedList = new ArrayList<>(resultSize);
        messageBufferList = new ArrayList<>(resultSize);
        messageQueueOffset = new ArrayList<>(resultSize);
    }

    /**
     * 构建查询消息结果对象。
     * @param status 查询消息结果状态
     * @param nextBeginOffset  下次查询起始偏移量
     * @param minOffset        当前结果中消息的最小偏移量
     * @param maxOffset        当前结果中消息的最大偏移量
     * @param messageMapedList    消息List集合
     * @param messageBufferList   消息ByteBuffer的List集合
     * @param messageQueueOffset  消息队列的偏移量List集合
     */
    private GetMessageResult(GetMessageStatus status, long nextBeginOffset, long minOffset, long maxOffset,
        List<SelectMappedBufferResult> messageMapedList, List<ByteBuffer> messageBufferList, List<Long> messageQueueOffset) {
        // 状态
        this.status = status;
        // 下一个请求应该开始的偏移量
        this.nextBeginOffset = nextBeginOffset;
        // 最小偏移量
        this.minOffset = minOffset;
        // 最大偏移量
        this.maxOffset = maxOffset;
        // messageMapedList
        this.messageMapedList = messageMapedList;
        // messageBufferList
        this.messageBufferList = messageBufferList;
        // messageQueueOffset
        this.messageQueueOffset = messageQueueOffset;
    }

    public GetMessageStatus getStatus() {
        return status;
    }

    public void setStatus(GetMessageStatus status) {
        this.status = status;
    }

    public long getNextBeginOffset() {
        return nextBeginOffset;
    }

    public void setNextBeginOffset(long nextBeginOffset) {
        this.nextBeginOffset = nextBeginOffset;
    }

    public long getMinOffset() {
        return minOffset;
    }

    public void setMinOffset(long minOffset) {
        this.minOffset = minOffset;
    }

    public long getMaxOffset() {
        return maxOffset;
    }

    public void setMaxOffset(long maxOffset) {
        this.maxOffset = maxOffset;
    }

    public List<SelectMappedBufferResult> getMessageMapedList() {
        return messageMapedList;
    }

    public List<ByteBuffer> getMessageBufferList() {
        return messageBufferList;
    }

    /**
     * 添加消息
     * @param mapedBuffer 需要添加的消息
     */
    public void addMessage(final SelectMappedBufferResult mapedBuffer) {
        // 添加指定消息
        this.messageMapedList.add(mapedBuffer);
        // 添加指定消息的ByteBuffer
        this.messageBufferList.add(mapedBuffer.getByteBuffer());
        // 缓冲区字节大小累加
        this.bufferTotalSize += mapedBuffer.getSize();
        //
        this.msgCount4Commercial += (int) Math.ceil(
            mapedBuffer.getSize() /  (double)commercialSizePerMsg);
        // 消息总数累加
        this.messageCount++;
    }

    /**
     * 向查询消息结果对象添加一条消息
     * @param mapedBuffer 需要添加的消息
     * @param queueOffset 查询消息队列偏移量
     */
    public void addMessage(final SelectMappedBufferResult mapedBuffer, final long queueOffset) {
        // 添加消息
        this.messageMapedList.add(mapedBuffer);
        // 添加消息的ByteBuffer
        this.messageBufferList.add(mapedBuffer.getByteBuffer());
        // 消息的字节数累加
        this.bufferTotalSize += mapedBuffer.getSize();
        // 消息数累加
        this.msgCount4Commercial += (int) Math.ceil(
            mapedBuffer.getSize() /  (double)commercialSizePerMsg);
        // 消息数累加
        this.messageCount++;
        // 添加消息偏移量
        this.messageQueueOffset.add(queueOffset);
    }

    /**
     * 向查询消息结果对象添加消息
     * @param mapedBuffer
     * @param queueOffset
     * @param batchNum
     */
    public void addMessage(final SelectMappedBufferResult mapedBuffer, final long queueOffset, final int batchNum) {
        addMessage(mapedBuffer, queueOffset);
        messageCount += batchNum - 1;
    }

    /**
     * 释放ByteBuffer
     */
    public void release() {
        for (SelectMappedBufferResult select : this.messageMapedList) {
            select.release();
        }
    }

    public int getBufferTotalSize() {
        return bufferTotalSize;
    }

    public int getMessageCount() {
        return messageCount;
    }

    public boolean isSuggestPullingFromSlave() {
        return suggestPullingFromSlave;
    }

    public void setSuggestPullingFromSlave(boolean suggestPullingFromSlave) {
        this.suggestPullingFromSlave = suggestPullingFromSlave;
    }

    public int getMsgCount4Commercial() {
        return msgCount4Commercial;
    }

    public void setMsgCount4Commercial(int msgCount4Commercial) {
        this.msgCount4Commercial = msgCount4Commercial;
    }

    public List<Long> getMessageQueueOffset() {
        return messageQueueOffset;
    }

    public long getColdDataSum() {
        return coldDataSum;
    }

    public void setColdDataSum(long coldDataSum) {
        this.coldDataSum = coldDataSum;
    }

    @Override
    public String toString() {
        return "GetMessageResult [status=" + status + ", nextBeginOffset=" + nextBeginOffset + ", minOffset="
            + minOffset + ", maxOffset=" + maxOffset + ", bufferTotalSize=" + bufferTotalSize + ", messageCount=" + messageCount
            + ", suggestPullingFromSlave=" + suggestPullingFromSlave + "]";
    }
}
